<?php
require_once(__PHY_LIB.'/class/Item.class.php');

class uWork extends Item {
/// definition and constructor ///
    function uWork($id=0, $field="") {
        Query::Query();
        $this->cfgKeyField("id");
        $this->cfgAllField("id,owner_id,title,category_id,price,thumb,tags,description,time_added,time_updated");
        $this->cfgTable("unit_work");
        if ("" == $field || "*" == $field) {
            $this->cfgField($this->mAllField);
        }
        else {
            $this->cfgField($field);
        }
        if (0 < $id) {
            $this->cfgWhere($this->mKeyField . "=" . $id);
            $this->load();
        }
        else {
            $this->set($this->mKeyField, $id);
        }
    }

    function invalid() {
        if (!$this->get('title')) {
            return 'title';
        }

        return false;
	}

	
/// 缩略图相关 ///
    function calcThumbUrl($nID, $sFile) {
		if (!(0 < $nID)) { $nID = $this->getID(); }
        if (!(0 < $nID)) { return false; }
        if (!$sFile) { return false; }
        $sNumber = str_pad($nID, 9, '0', STR_PAD_LEFT);
		$sDir = "/".substr($sNumber, 0, 3)."/".substr($sNumber, 3, 3)."/".substr($sNumber, 6, 3)."/";
		return "/work{$sDir}{$sFile}";
    }
    function saveThumb($sSrcPath) {
		ini_set('memory_limit', '512M'); // 为了图片处理，临时放开内存限制。
        /// 得到图片信息 ///
        list($nOldW, $nOldH, $nType) = getimagesize($sSrcPath);
        if (IMAGETYPE_GIF == $nType) {
			$hOld = imagecreatefromgif($sSrcPath);
			$sExt = 'gif';
        }
        elseif (IMAGETYPE_JPEG == $nType) {
            $hOld = imagecreatefromjpeg($sSrcPath);
			$sExt = 'jpg';
        }
        elseif (IMAGETYPE_PNG == $nType) {
            $hOld = imagecreatefrompng($sSrcPath);
			$sExt = 'png';
        }
		else { return false; } // 只支持gif/jpg/png
		/// 保存源文件 ///
		if (!($sUrl = $this->calcThumbUrl(null, "source.{$sExt}"))) { return false; }
		mkdir_plus(dirname(__PHY_STO.$sUrl)); // 生成目录
		move_uploaded_file($sSrcPath, __PHY_STO.$sUrl); // 保存
		$sSrcPath = __PHY_STO.$sUrl;
		/// 生成图片 ///
		$rTypes = array(
			'100x100.jpg',
			'50x50.jpg',
		);
		$cnt = 0;
		foreach ($rTypes as $sFile) {
			if (!($sUrl = $this->calcThumbUrl(null, $sFile))) { continue; }
			mkdir_plus(dirname(__PHY_STO.$sUrl)); // 生成目录
			preg_match('/^(\d+)x(\d+)\.(\w+)$/', $sFile, $r);
			list($x, $nNewW, $nNewH, $sExt) = $r;
			$hNew = imagecreatetruecolor($nNewW, $nNewH);
			$cWhite = imagecolorallocate($hNew, 255, 255, 255);
			imagefilledrectangle($hNew, 0, 0, $nNewW, $nNewH, $cWhite); // 画一个白底
			list($nFillW, $nFillH) = scale_rectangle(array($nOldW, $nOldH), array($nNewW, $nNewH));
			imagecopyresampled($hNew, $hOld,
				floor(($nNewW - $nFillW)/2), floor(($nNewH - $nFillH)/2), 0, 0,
				$nFillW, $nFillH, $nOldW, $nOldH);
			imagejpeg($hNew, __PHY_STO.$sUrl); // 保存
			imagedestroy($hNew);
			$cnt++;
		}
		imagedestroy($hOld);
		/// 标记已有图片 ///
		if ($cnt == sizeof($rTypes)) {
        	$this->set('thumb', 1);
			return true;
		}
		else { return false; }
	}

} /// end class uWork.


class uWorkList extends ItemList {
    /// definition and constructor ///
    function uWorkList($tablename=null) {
        ItemList::ItemList();
        $this->cfgClass("uWork");
        $this->cfgKeyField("id");
        $this->cfgAllField("id,owner_id,title,category_id,price,thumb,tags,description,time_added,time_updated");
        $this->cfgTable(null == $tablename ? "unit_work" : $tablename);
    }

} /// end class uWorkList.


/// related functions ///
function uWork_thumb($bHasThumb, $nID, $sFile) {
	if ($bHasThumb) {
		$sUrl = uWork::calcThumbUrl($nID, $sFile);
	}
	else { $sUrl = "/work/default/{$sFile}"; }
	return __DIR_STO.$sUrl;
}
?>
